/****************************************************
          File Name:server.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Fri 12 Sep 2014 08:19:22 PM CST
*****************************************************/

#include "msg.h"
#include <string.h>
#include <fcntl.h>

int main(int argc,char *argv[])
{
	struct msgbuf msg;
	msg.mtype=1;
	int id;
	char *buf;
	
	buf=malloc(DATA_SIZE);
	
	if((id=msgget(key,S_IRUSR))==-1)
		err("get id");
	
	for(;;)
	{
		fgets(buf,DATA_SIZE,stdin);
		if(buf[0]=='\n')
		{
			free(buf);
			exit(0);
		}
		if(strcpy(msg.mtext,buf)==NULL)
			err("copy to mtext");
		if(msgsnd(id,&msg,strlen(msg.mtext),0)==-1)
			err("send to server");
		if(msgrcv(id,&msg,DATA_SIZE,0,0)==-1)		// block while no msgq
			err("recive from server");
		printf("%s\n",msg.mtext);
	}
	
	exit(0); 	// will never run
}